package com.qyl.honghengshop.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.qyl.honghengshop.dao.BaseDao;
import com.qyl.honghengshop.dao.GoodsDao;
import com.qyl.honghengshop.dto.Goods;
import com.qyl.honghengshop.dto.OrderItem;
import com.qyl.honghengshop.mapper.GoodsMapper;
import com.qyl.honghengshop.mapper.OrderItemMapper;
import com.qyl.honghengshop.mapper.TimeMapper;
import com.qyl.honghengshop.utils.PageBean;

public class GoodsDaoImpl extends BaseDao implements GoodsDao {

	@Override
	public int getRecordCount(Goods goods) {
		
		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		
		GoodsMapper shopMapper = sqlSession.getMapper(GoodsMapper.class);
		
		int count = shopMapper.getRecordCount(goods);
		
		//关闭sqlSession
		this.closeSqlSession();
	
		return count;
	}

	@Override
	public void shopTypeList(PageBean pageBean, Goods goods) {

		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		
		GoodsMapper shopMapper = sqlSession.getMapper(GoodsMapper.class);
		
		List<Goods> shopList = shopMapper.shopList(pageBean,goods);
		
		pageBean.setList(shopList);
		
		//关闭sqlSession
		this.closeSqlSession();
		
	}

	@Override
	public int saveShop(Goods goods) {

		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		
		GoodsMapper shopMapper = sqlSession.getMapper(GoodsMapper.class);
		
		int count = shopMapper.saveShop(goods);
		
		sqlSession.commit();
		
		//关闭sqlSession
		this.closeSqlSession();
		
		return count;
	}

	@Override
	public int delShop(Integer id) {
		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		int count = 0;
		try {
			//把关联商品的外键全部置为空
			TimeMapper timeMapper = sqlSession.getMapper(TimeMapper.class);
			
			timeMapper.deleteGoodsTimeById(id);
			
			OrderItemMapper orderItemMapper = sqlSession.getMapper(OrderItemMapper.class);
			
			orderItemMapper.deleteOrderItemById(id);
			
			GoodsMapper shopMapper = sqlSession.getMapper(GoodsMapper.class);
			
			count = shopMapper.delShop(id);
			
			sqlSession.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		
		}
		
		//关闭sqlSession
		this.closeSqlSession();
		
		return count;
	}

	@Override
	public Goods getShopById(Integer id) {
		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		
		GoodsMapper shopMapper = sqlSession.getMapper(GoodsMapper.class);
		
		Goods good = shopMapper.getShopById(id);
		
		//关闭sqlSession
		this.closeSqlSession();
		
		return good;
	}

	@Override
	public int updateShop(Goods goods) {

		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		
		GoodsMapper shopMapper = sqlSession.getMapper(GoodsMapper.class);
		
		int count = shopMapper.updateShop(goods);
		
		sqlSession.commit();
		
		//关闭sqlSession
		this.closeSqlSession();
		
		return count;
	}

	@Override
	public List<Goods> getAllShop() {

		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		
		GoodsMapper shopMapper = sqlSession.getMapper(GoodsMapper.class);
		
		List<Goods> shopList = shopMapper.getAllShop();
	
		//关闭sqlSession
		this.closeSqlSession();
		
		return shopList;
	}

	@Override
	public List<Goods> getGoodsListIsHot() {

		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		
		GoodsMapper shopMapper = sqlSession.getMapper(GoodsMapper.class);
		
		List<Goods> shopList = shopMapper.getGoodsListIsHot();
	
		//关闭sqlSession
		this.closeSqlSession();
		
		return shopList;

	}

	@Override
	public List<Goods> getGoodsByCode(String code) {

		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		
		GoodsMapper shopMapper = sqlSession.getMapper(GoodsMapper.class);
		
		List<Goods> shopList = shopMapper.getGoodsByCode(code);
	
		//关闭sqlSession
		this.closeSqlSession();
		
		return shopList;
	
	}


}
